<?php

class ProdajaController extends Zend_Controller_Action
{

    public function preDispatch()
    {
        $this->view->render('prodaja/_navigation.phtml');
    }

    public function init()
    {
        $this->_helper->layout->setLayout('prodaja');
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');
        $sellerSession->setExpirationSeconds(3600);
    }

    public function indexAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if ($sellerSession->sellerId) {
            $this->_helper->redirector('dashboard', 'prodaja');
            return;
        }
        $form    = new Application_Form_Admin();
        $this->view->form = $form;
    }

    public function loginAction()
    {
        $loginForm = new Application_Form_Admin();

        if ($loginForm->isValid($_POST)) {
            $sellerSession = new Zend_Session_Namespace('Store_Sellers');
            $sellerSession->setExpirationSeconds(3600);
            $emso = $loginForm->getValue('emso');
            $geslo = $loginForm->getValue('geslo');
            $sellerMapper = new Application_Model_SellerMapper();

            $sellerUser = $sellerMapper->authenticate($emso, $geslo);

            if ($sellerUser != null) {
                $sellerSession->sellerId = $sellerUser->getEmso();
                $sellerSession->ime = $sellerUser->getIme()." ".$sellerUser->getPriimek();
                $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
                $logger = new Zend_Log($writer);

                $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." se je prijavil");
            } else {
                $sellerSession->error = "Napacni podatki!";
            }
            $this->_helper->redirector('index', 'prodaja');
        }
    }

    public function dashboardAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');
        if (!$sellerSession->sellerId) {
            $this->_helper->redirector('index', 'prodaja');
        }
    }

    public function logoutAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');
        if (!$sellerSession->sellerId) {
            $this->_helper->redirector('index', 'prodaja');

        }
        $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
        $logger = new Zend_Log($writer);

        $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." se je odjavil");

        $sellerSession->unsetAll();

        $this->_helper->redirector('index', 'prodaja');
    }

    public function editAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if (!$sellerSession->sellerId) {
            $this->_helper->redirector('index', 'prodaja');
            return;
        }

        $seller = new Application_Model_Seller();
        $sellerMapper = new Application_Model_SellerMapper();

        $sellerMapper->find($sellerSession->sellerId, $seller);
        if ($seller->getEmso()) {
            $data = array(
                "ime" => $seller->getIme(),
                "priimek" => $seller->getPriimek(),
            );

            $form    = new Application_Form_AdminEdit();
            $form->setDefaults($data);
            $this->view->form = $form;
        } else {
            $this->_helper->redirector('index', 'prodaja');
            return;
        }
    }

    public function editPostAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');
        $editForm = new Application_Form_AdminEdit();
        $emso =  $sellerSession->sellerId;

        if ($sellerSession->sellerId) {
            // Polja v $editForm se napolnejo šele po klicu isValid
            if ($editForm->isValid($_POST)) {
                $geslo = $editForm->getValue('geslo');
                $sellerMapper = new Application_Model_SellerMapper();
                $sellerUser = $sellerMapper->authenticate($emso, $geslo);
                if ($sellerUser != null) {
                    if ($editForm->getValue('spremeni_geslo')) {
                        if (strlen(trim($editForm)) > 0 && $editForm->getValue('geslo_novo') == $editForm->getValue('geslo_potrditev')) {
                            $sellerUser->setPriimek($editForm->getValue('priimek'));
                            $sellerUser->setIme($editForm->getValue('ime'));
                            $sellerUser->setGeslo(password_hash($editForm->getValue('geslo_novo'), PASSWORD_DEFAULT));
                            $sellerMapper->save($sellerUser);
                            $sellerSession->success = "Shranjevanje uspešno";
                            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
                            $logger = new Zend_Log($writer);

                            $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je posodobil svoje podatke");

                        } else {
                            $sellerSession->error = "Napaka pri nastavljanju novega gesla. Prosim preveri podatke.";
                        }
                    } else {
                        $sellerUser->setPriimek($editForm->getValue('priimek'));
                        $sellerUser->setIme($editForm->getValue('ime'));
                        $sellerMapper->save($sellerUser);
                        $sellerSession->success = "Shranjevanje uspešno";
                        $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
                        $logger = new Zend_Log($writer);

                        $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je posodobil svoje podatke");
                    }
                } else {
                    $sellerSession->error = "Napačno geslo!";
                }
            }
        } else {
            $sellerSession->error = "Ta akcija je na voljo samo prodajalcem!";
        }
        $this->_helper->redirector('edit', 'prodaja');
    }

    public function customersGridAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');
        if ($sellerSession->sellerId) {

        } else {
            $sellerSession->error = "Ta akcija je na voljo samo prodajalcu!";
            $this->_helper->redirector('index', 'index');
        }
    }

    public function ordersGridAction()
    {
        // action body
    }

    public function productsGridAction()
    {
        // action body
    }

    public function addProductAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if (!$sellerSession->sellerId) {
            $this->_helper->redirector('index', 'prodaja');
            return;
        }
        $form    = new Application_Form_AddProduct();
        $this->view->form = $form;
    }

    public function addProductPostAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if ($sellerSession->sellerId) {

            $editForm = new Application_Form_AddProduct();

            // Polja v $editForm se napolnejo šele po klicu isValid
            if ($editForm->isValid($_POST)) {
                $id = $editForm->getValue('id_produkt');
                $ime = $editForm->getValue('ime');
                $cena = $editForm->getValue('cena');
                $zaloga = $editForm->getValue('zaloga');
                $base_image = $editForm->getValue('base_image');
                $thumbnail = $editForm->getValue('thumbnail');

                $izdelekMapper = new Application_Model_IzdelkiMapper();

                $izdelek = new Application_Model_Izdelki();

                $izdelek->setIdProdukt($id);
                $izdelek->setIme($ime);
                $izdelek->setCena($cena);
                $izdelek->setZaloga($zaloga);
                $izdelek->setBaseImage($base_image);
                $izdelek->setThumbNAil($thumbnail);

                try {
                    $izdelekMapper->save($izdelek);
                    $sellerSession->success = "Shranjevanje izdelka uspešno";

                    $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
                    $logger = new Zend_Log($writer);

                    $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je dodal ali uredil produkt z id-jem ".$id);
                } catch (Exception $e) {
                    $sellerSession->error = $e->getMessage();
                }
            } else {
                $sellerSession->error = "Neveljaven post!";
            }
        } else {
            $sellerSession->error = "Ta akcija je na voljo samo prodajalcu!";
        }
        $this->_helper->redirector('products-grid', 'prodaja');
    }

    public function createCustomerAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if (!$sellerSession->sellerId) {
            $this->_helper->redirector('index', 'prodaja');
            return;
        }
        $form    = new Application_Form_NovaStranka();
        $this->view->form = $form;
    }

    public function createCustomerPostAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if ($sellerSession->sellerId) {

            $editForm = new Application_Form_NovaStranka();

            // Polja v $editForm se napolnejo šele po klicu isValid
            if ($editForm->isValid($_POST)) {
                $emso = $editForm->getValue('emso');
                $geslo = $editForm->getValue('geslo');
                $strankaMapper = new Application_Model_StrankaMapper();

                $stranka = new Application_Model_Stranka();

                $stranka->setEmso($emso);
                $stranka->setGeslo(password_hash($geslo, PASSWORD_DEFAULT));
                $stranka->setPriimek($editForm->getValue('priimek'));
                $stranka->setIme($editForm->getValue('ime'));
                $stranka->setTelefon($editForm->getValue('telefon'));
                $stranka->setEmail($editForm->getValue('email'));
                $stranka->setNaslov($editForm->getValue('naslov'));

                try {
                    $strankaMapper->save($stranka);
                    $sellerSession->success = "Shranjevanje stranke uspešno";
                    $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
                    $logger = new Zend_Log($writer);

                    $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je dodal stranko z EMŠO: ".$stranka->getEmso());
                } catch (Exception $e) {
                    $sellerSession->error = $e->getMessage();
                }
            } else {
                $sellerSession->error = "Neveljaven post!";
            }
        } else {
            $sellerSession->error = "Ta akcija je na voljo samo prodajalcu!";
        }
        $this->_helper->redirector('customers-grid', 'prodaja');
    }

    public function editStrankaAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if ($sellerSession->sellerId) {
            $form = new Application_Form_NovaStranka();

            $strankaEmso = base64_decode($this->getParam('id'));

            $stranka = new Application_Model_Stranka();
            $strankaMapper = new Application_Model_StrankaMapper();

            $strankaMapper->find($strankaEmso, $stranka);

            $data = array(
                "emso" => $stranka->getEmso(),
                "ime" => $stranka->getIme(),
                "priimek" => $stranka->getPriimek(),
                "naslov" => $stranka->getNaslov(),
                "telefon" => $stranka->getTelefon(),
                "email" => $stranka->getEmail()
            );

            $form->setDefaults($data);
            $this->view->form = $form;
        } else {
            $sellerSession->error = "Ta akcija je na voljo samo prodajalcu!";
            $this->_helper->redirector('customers-grid', 'prodaja');

        }
    }

    public function deleteStrankaAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');
        if ($sellerSession->sellerId) {
            $emso = base64_decode($this->getParam('id'));
            $strankaMapper = new Application_Model_StrankaMapper();
            $strankaMapper->delete($emso);

            $sellerSession->success = "Brisanje stranke uspešno";
            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);

            $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je izbrisal stranko z EMŠO: ".$emso);
        } else {
            $sellerSession->error = "Ta akcija je na voljo samo prodajalcu!";
        }
        $this->_helper->redirector('customers-grid', 'prodaja');
    }

    public function aktivirajStrankoAction()
    {
        $form    = new Application_Form_IdForm();
        $form->setDefaults(array('id' => $this->getParam('id')));
        $this->view->form = $form;
    }

    public function deaktivirajStrankoAction()
    {
        $form    = new Application_Form_IdForm();
        $form->setDefaults(array('id' => $this->getParam('id')));
        $this->view->form = $form;
    }

    public function uploadImageAction()
    {
        $this->_helper->layout->disableLayout();
        $upload_handler = new Application_Model_UploadHandler();
    }

    public function editProductAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if (!$sellerSession->sellerId && !$this->getParam('id')) {
            $this->_helper->redirector('index', 'prodaja');
            return;
        }
        $form    = new Application_Form_AddProduct();

        $izdelekMapper = new Application_Model_IzdelkiMapper();
        $izdelek = new Application_Model_Izdelki();

        $izdelekMapper->find(base64_decode($this->getParam('id')), $izdelek);

        $data = array(
            "id_produkt" => $izdelek->getIdProdukt(),
            "ime" => $izdelek->getIme(),
            "cena" => $izdelek->getCena(),
            "zaloga" => $izdelek->getZaloga(),
            "base_image" => $izdelek->getBaseImage(),
            "thumbnail" => $izdelek->getThumbNail(),
        );

        $form->setDefaults($data);
        $this->view->form = $form;
    }

    public function deleteProductAction()
    {
        $form    = new Application_Form_IdForm();
        $form->setDefaults(array('id' => $this->getParam('id')));
        $this->view->form = $form;
    }

    public function deleteProductPostAction()
    {
        $idForm = new Application_Form_IdForm();

        if ($idForm->isValid($_POST)) {
            $sellerSession = new Zend_Session_Namespace('Store_Sellers');

            $id = base64_decode($idForm->getValue('id'));
            $izdelekMapper = new Application_Model_IzdelkiMapper();

            $izdelekMapper->delete($id);

            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);

            $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je izbrisal produkt z id-jem: ".$id);

            $this->_helper->redirector('products-grid', 'prodaja');
             return true;
        }

        $this->_helper->redirector('index', 'prodaja');
        return false;
    }

    public function activateProductAction()
    {
        $form    = new Application_Form_IdForm();
        $form->setDefaults(array('id' => $this->getParam('id')));
        $this->view->form = $form;
    }

    public function activateProductPostAction()
    {
        $idForm = new Application_Form_IdForm();
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if ($idForm->isValid($_POST)) {
            $id = base64_decode($idForm->getValue('id'));
            $izdelekMapper = new Application_Model_IzdelkiMapper();

            $izdelekMapper->update($id, "aktiviran", "1");
            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);
            $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je aktiviral produkt z id-jem: ".$id);

            $this->_helper->redirector('products-grid', 'prodaja');

            return true;
        }

        $this->_helper->redirector('index', 'prodaja');
        return false;
    }

    public function deactivateProductPostAction()
    {

        $idForm = new Application_Form_IdForm();
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if ($sellerSession->sellerId && $idForm->isValid($_POST)) {
            $id = base64_decode($idForm->getValue('id'));
            $izdelekMapper = new Application_Model_IzdelkiMapper();

            $izdelekMapper->update($id, "aktiviran", "0");
            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);
            $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je deaktiviral produkt z id-jem: ".$id);

            $this->_helper->redirector('products-grid', 'prodaja');

            return true;
        } else {
            $sellerSession->error = "Ta akcija je na voljo samo prodajalcu!";
        }

        $this->_helper->redirector('index', 'prodaja');
        return false;
    }

    public function deactivateProductAction()
    {
        $form    = new Application_Form_IdForm();
        $form->setDefaults(array('id' => $this->getParam('id')));
        $this->view->form = $form;
    }

    public function aktivirajStrankoPostAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');
        $idForm = new Application_Form_IdForm();

        if ($sellerSession->sellerId && $idForm->isValid($_POST)) {
            $id = base64_decode($idForm->getValue('id'));
            $strankaMapper = new Application_Model_StrankaMapper();
            $strankaMapper->update($id, "aktiviran", 1);

            $sellerSession->success = "Aktivacija stranke uspešna";
            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);
            $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je aktiviral stranko z EMŠO: ".$id);

        } else {
            $sellerSession->error = "Ta akcija je na voljo samo prodajalcu!";
        }
        $this->_helper->redirector('customers-grid', 'prodaja');

    }

    public function deaktivirajStrankoPostAction()
    {
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');
        $idForm = new Application_Form_IdForm();

        if ($sellerSession->sellerId && $idForm->isValid($_POST)) {
            $id = base64_decode($idForm->getValue('id'));
            $strankaMapper = new Application_Model_StrankaMapper();
            $strankaMapper->update($id, "aktiviran", 0);

            $sellerSession->success = "Deaktivacija stranke uspešna";
            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);
            $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je deaktiviral stranko z EMŠO: ".$id);

        } else {
            $sellerSession->error = "Ta akcija je na voljo samo prodajalcu!";
        }
        $this->_helper->redirector('customers-grid', 'prodaja');
    }

    public function posodobiNarociloAction()
    {
        $id = $this->getParam('id');
        $narociloModel = new Application_Model_Narocilo();
        $narociloModel->loadById($id);
        $form = new Application_Form_NarociloForm();
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');


        $opcije = array();

        if ($narociloModel->getStatus() == Application_Model_Narocilo::STATUS_NOV) {
            $opcije = array(
                Application_Model_Narocilo::STATUS_NOV => $narociloModel->oznakaStatusa(Application_Model_Narocilo::STATUS_NOV),
                Application_Model_Narocilo::STATUS_ODOBRENO => $narociloModel->oznakaStatusa(Application_Model_Narocilo::STATUS_ODOBRENO),
                Application_Model_Narocilo::STATUS_ZAVRNJENO => $narociloModel->oznakaStatusa(Application_Model_Narocilo::STATUS_ZAVRNJENO),
            );
        } elseif ($narociloModel->getStatus() == Application_Model_Narocilo::STATUS_ODOBRENO) {
            $opcije = array(
                Application_Model_Narocilo::STATUS_ODOBRENO => $narociloModel->oznakaStatusa(Application_Model_Narocilo::STATUS_ODOBRENO),
                Application_Model_Narocilo::STATUS_STORNIRANO => $narociloModel->oznakaStatusa(Application_Model_Narocilo::STATUS_STORNIRANO),
            );
        } else {
            $sellerSession->error = 'Statusa naročil v statusu '.$narociloModel->oznakaStatusa($narociloModel->getStatus()).' ni več mogoče posodabljati';
            $this->_helper->redirector('orders-grid', 'prodaja');
            return;
        }

        $form->getElement('status')->addMultiOptions($opcije);

        $defaults = array(
            'status' => $narociloModel->getStatus(),
            'id'     => $id
        );

        $form->setDefaults($defaults);

        $this->view->form = $form;
    }

    public function posodobiNarociloPostAction()
    {
        $idForm = new Application_Form_NarociloForm();
        $sellerSession = new Zend_Session_Namespace('Store_Sellers');

        if ($idForm->isValid($_POST) && $sellerSession->sellerId) {
            $idNarocila = $idForm->getValue('id');
            $novStatus = $idForm->getValue('status');

            $narocilo = new Application_Model_Narocilo();
            $narocilo->loadById($idNarocila);
            $starStatus = $narocilo->getStatus();
            $narocilo->posodobiStatus($novStatus);

            if ($novStatus == Application_Model_Narocilo::STATUS_STORNIRANO || $novStatus == Application_Model_Narocilo::STATUS_ZAVRNJENO) {
                $sellerSession->success = 'Naročilo uspešno stornirano/zavrnjeno. Zaloga produktov je bila posodobljena';
                $narocilo->rollbackZaloga();
            }

            $writer = new Zend_Log_Writer_Stream('../var/log/ActionLog.txt');
            $logger = new Zend_Log($writer);
            $logger->info("Prodajalec: ".$sellerSession->ime." EMŠO: ".$sellerSession->sellerId." je posodobil naročilo št. ".$idNarocila. " iz statusa ".$starStatus." na status ".$novStatus);


            $this->_helper->redirector('orders-grid', 'prodaja');

        } else {
            $sellerSession->error = 'Napaka!';
            $this->_helper->redirector('orders-grid', 'prodaja');

        }
    }

    public function viewNarociloAction()
    {
        $narocilo = new Application_Model_Narocilo();
        $narocilo->loadById($this->getParam('id'));

        $this->view->narocilo = $narocilo;
    }


}































































